layui.config({
	base : "js/"
}).use(['form','layer','jquery','laypage'],function(){
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage,
		$ = layui.jquery;

	//加载页面数据
	var linksData = '';
	$.ajax({
		url : "/admin/getSinglePageList.json",
		type : "get",
		dataType : "json",
		success : function(resp){
			//执行加载数据的方法
			fillData(resp.data);
		}
	})

	//添加
	$(".linksAdd_btn").click(function(){
		var index = layui.layer.open({
			title : "添加单页信息",
			type : 2,
			content : "/admin/page/singlePage/singlePageAdd.html",
			success : function(layero, index){
				layui.layer.tips('点击此处返回文章列表', '.layui-layer-setwin .layui-layer-close', {
					tips: 3
				});
			}
		})
		//改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
		$(window).resize(function(){
			layui.layer.full(index);
		})
		layui.layer.full(index);
	})

	//批量删除
	$(".batchDel").click(function(){
		var $checkbox = $('.links_list tbody input[type="checkbox"][name="checked"]');
		var $checked = $('.links_list tbody input[type="checkbox"][name="checked"]:checked');
		if($checkbox.is(":checked")){
			layer.confirm('确定删除选中的信息？',{icon:3, title:'提示信息'},function(index){
				var index = layer.msg('暂未开放此功能',{icon: 16,time:1000,shade:0.8});
	        })
		}else{
			layer.msg("请选择需要删除的文章");
		}
	})

	//全选
	form.on('checkbox(allChoose)', function(data){
		var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"])');
		child.each(function(index, item){
			item.checked = data.elem.checked;
		});
		form.render('checkbox');
	});

	//通过判断文章是否全部选中来确定全选按钮是否选中
	form.on("checkbox(choose)",function(data){
		var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"])');
		var childChecked = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"]):checked')
		data.elem.checked;
		if(childChecked.length == child.length){
			$(data.elem).parents('table').find('thead input#allChoose').get(0).checked = true;
		}else{
			$(data.elem).parents('table').find('thead input#allChoose').get(0).checked = false;
		}
		form.render('checkbox');
	})
 
	//操作
	$("body").on("click",".links_edit",function(){  //编辑
		var _this = this;
		layer.msg("暂时不允许编辑");
		retunr;
        var index = layui.layer.open({
            title : "编辑",
            type : 2,
            content : "/admin/editSinglePage/" + $(_this).data("id")
        })
        //改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
        $(window).resize(function(){
            layui.layer.full(index);
        })
        layui.layer.full(index);
	})

	$("body").on("click",".links_del",function(){  //删除
		var _this = $(this);
		layer.confirm('确定删除此栏目？',{icon:3, title:'提示信息'},function(index){

			$.get("/admin/deleteSinglePage/"+$(_this).data("id"),function(resp){
				if(resp.code == '0'){
                    _this.parents("tr").remove();
                    layer.msg(resp.msg);
				}
			});
			// layer.close(index);
		});
	})

	function fillData(that){
		//渲染数据
		function renderDate(data){
			var currData = data;
			var dataHtml = '';
			if(currData.length != 0){
				for(var i=0;i<currData.length;i++){
					dataHtml += '<tr>'
			    	+'<td><input type="checkbox" name="checked" lay-skin="primary" lay-filter="choose"></td>'
			    	+'<td align="left">'+(i+1)+'</td>'
			    	+'<td align="left">'+(currData[i].pid==0?'':'--/')+currData[i].name+'</td>'
			    	+'<td>'+currData[i].sort+'</td>'
			    	+'<td>'+(currData[i].type==''?'':(currData[i].type=='1'?'单页文章栏目':(currData[i].type=='2'?'普通文章栏目':(currData[i].type=='3'?'图片栏目':'unknown'))))+'</td>'
			    	+'<td>'+currData[i].updateTime+'</td>'
			    	+'<td>'
					+  '<a class="layui-btn layui-btn-mini links_edit"  data-id="'+data[i].categoryId+'"><i class="layui-icon">&#xe642;</i> 编辑</a>'
			        +'</td>'
			    	+'</tr>';
				}
			}else{
				dataHtml = '<tr><td colspan="7">暂无数据</td></tr>';
			}
            $(".content").html(dataHtml);
            form.render();
		    return dataHtml;
		}
        renderDate(that);
	}
})
